package com.example.mymvvmdemo.models.add_device.fragment.bind_device;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadFragment;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.FragmentBindDeviceBinding;
import com.example.mymvvmdemo.models.add_device.AddDeviceViewModel;
import com.example.mymvvmdemo.net.presenter.PostBodyPresenter;
import com.example.mymvvmdemo.net.presenter.PostPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class BindDeviceFragment extends BaseLoadFragment<FragmentBindDeviceBinding> implements View.OnClickListener, IView.PostBodyView {

    private AddDeviceViewModel viewModel;
    private PostBodyPresenter postBodyPresenter;

    @Override
    protected void initData() {
        postBodyPresenter = new PostBodyPresenter(this);
    }

    @Override
    protected void initListener() {
        binding.tvNext.setOnClickListener(this);
        binding.ivClearId.setOnClickListener(this);

        binding.etId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.ivClearId.setVisibility(charSequence.toString().length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_next:
                startCheckDevice();
                break;
            case R.id.iv_clear_id:
                binding.etId.setText("");
                break;
            default:
                break;
        }
    }

    /***********************************自定义方法**************************************/
    public void setViewModel(AddDeviceViewModel viewModel) {
        this.viewModel = viewModel;
    }

    private void startCheckDevice() {

        String id = binding.etId.getText().toString();
        viewModel.id = id;

        if (StrUtils.isEmpty(id)) {
            toast(R.string.add_device_id_mac, ToastUtils.TOA_W);
            return;
        }

        startBindDevice();
    }

    private void startBindDevice() {

        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        if (viewModel.id.contains(":")) {
            hashMap.put("mac_address", "" + viewModel.id);
        } else {
            hashMap.put("device_id", "" + viewModel.id);
        }
        // 创建JSON对象来确保正确的格式
        JSONObject jsonObject = new JSONObject();
        RequestBody body = null;
        try {
            if (viewModel.id.contains(":")) {
                jsonObject.put("mac_address", "" + viewModel.id);
            } else {
                jsonObject.put("device_id", "" + viewModel.id);
            }
            body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        } catch (Exception e) {
        }
        postBodyPresenter.startLoadPostBody(UrlConstants.deviceBind, hashMap, body, UrlTypeConstants.deviceBind);

    }

    /***********************************数据请求**************************************/
    @Override
    public void loadPostBodySuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.deviceBind)) {
            loadDeviceBindSuccess(result, code);
        }
    }

    @Override
    public void loadPostBodyFail(String message, String type) {
        if (type.equals(UrlTypeConstants.deviceBind)) {
            loadDeviceBindFail();
        }
    }

    @Override
    public void loadPostBodyAccountError(int code) {

    }

    @Override
    public void loadPostBodyNetWorkError(String type) {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    /***********************************处理请求后数据**************************************/
    private void loadDeviceBindSuccess(String result, int code) {
        if (code != 200) {
            loadDeviceBindFail();
            return;
        }
        dismissDialog();
        toast(R.string.add_device_binding_success, ToastUtils.TOA_S);

    }

    private void loadDeviceBindFail() {
        dismissDialog();
        toast(R.string.add_device_binding_fail, ToastUtils.TOA_W);

    }

}
